package com.easychat.test;

import java.io.*;
import java.net.Socket;
import java.util.Optional;
import java.util.Scanner;

public class SocketClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 1024);
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        System.out.println("请输入内容");
        new Thread(()->{
            while(true){
                Scanner scanner = new Scanner(System.in);
                String s = scanner.nextLine();
                out.println(s);
                out.flush();
            }
        }).start();

        InputStream inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        new Thread(()->{
            String readLine = null;
            while (true){
                try {
                    readLine = bufferedReader.readLine();
                    System.out.println("收到服务端消息:"+readLine);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
}
